/*     */ package com.zimbra.cs.account.accesscontrol;
/*     */ 
/*     */ import com.google.common.base.Objects;
/*     */ import com.google.common.collect.HashMultimap;
/*     */ import com.google.common.collect.ImmutableList;
/*     */ import com.google.common.collect.ImmutableList.Builder;
/*     */ import com.google.common.collect.ImmutableMap;
/*     */ import com.google.common.collect.ImmutableMap.Builder;
/*     */ import com.google.common.collect.Multimap;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.AttributeClass;
/*     */ import com.zimbra.cs.account.Entry;
/*     */ import com.zimbra.cs.account.Identity;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.accesscontrol.generated.UserRights;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Collections;
/*     */ import java.util.EnumSet;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.Set;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public final class ACLUtil
/*     */ {
/*     */   private static final String ACL_CACHE_KEY = "ENTRY.ACL_CACHE";
/*     */   
/*     */   public static List<ZimbraACE> getAllACEs(Entry entry)
/*     */     throws ServiceException
/*     */   {
/*  52 */     ZimbraACL acl = getACL(entry);
/*  53 */     return acl != null ? acl.getAllACEs() : null;
/*     */   }
/*     */   
/*     */   public static Set<ZimbraACE> getAllowedNotDelegableACEs(Entry entry) throws ServiceException
/*     */   {
/*  58 */     ZimbraACL acl = getACL(entry);
/*  59 */     return acl != null ? acl.getAllowedNotDelegableACEs() : null;
/*     */   }
/*     */   
/*     */   public static Set<ZimbraACE> getAllowedDelegableACEs(Entry entry) throws ServiceException
/*     */   {
/*  64 */     ZimbraACL acl = getACL(entry);
/*  65 */     return acl != null ? acl.getAllowedDelegableACEs() : null;
/*     */   }
/*     */   
/*     */   public static Set<ZimbraACE> getDeniedACEs(Entry entry) throws ServiceException
/*     */   {
/*  70 */     ZimbraACL acl = getACL(entry);
/*  71 */     return acl != null ? acl.getDeniedACEs() : null;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static List<ZimbraACE> getACEs(Entry entry, Set<? extends Right> rights)
/*     */     throws ServiceException
/*     */   {
/*  83 */     ZimbraACL acl = getACL(entry);
/*  84 */     return acl != null ? acl.getACEs(rights) : null;
/*     */   }
/*     */   
/*     */   private static Multimap<Right, Entry> getGrantedRights(Account grantee, Set<String> fetchAttrs) throws ServiceException
/*     */   {
/*  89 */     SearchGrants search = new SearchGrants(grantee.getProvisioning(), EnumSet.of(TargetType.account), RightBearer.Grantee.getGrantee(grantee, false).getIdAndGroupIds());
/*     */     
/*  91 */     search.addFetchAttribute(fetchAttrs);
/*  92 */     Set<SearchGrants.GrantsOnTarget> results = search.doSearch().getResults();
/*  93 */     Multimap<Right, Entry> map = HashMultimap.create();
/*  94 */     for (Iterator i$ = results.iterator(); i$.hasNext();) { grants = (SearchGrants.GrantsOnTarget)i$.next();
/*  95 */       ZimbraACL acl = grants.getAcl();
/*  96 */       for (ZimbraACE ace : acl.getAllACEs()) {
/*  97 */         if (ace.getGrantee().equals(grantee.getId()))
/*  98 */           map.put(ace.getRight(), grants.getTargetEntry());
/*     */       }
/*     */     }
/*     */     SearchGrants.GrantsOnTarget grants;
/* 102 */     return map;
/*     */   }
/*     */   
/*     */ 
/*     */   public static List<Identity> getSendOnBehalfOf(Account grantee)
/*     */     throws ServiceException
/*     */   {
/* 109 */     Multimap<Right, Entry> rights = getGrantedRights(grantee, Collections.singleton("displayName"));
/* 110 */     ImmutableList.Builder<Identity> result = ImmutableList.builder();
/* 111 */     for (Entry entry : rights.get(UserRights.R_sendOnBehalfOf)) {
/* 112 */       Account grantor = (Account)entry;
/* 113 */       String mail = grantor.getName();
/* 114 */       String name = (String)Objects.firstNonNull(grantor.getDisplayName(), mail);
/* 115 */       Map<String, Object> attrs = ImmutableMap.builder().put("zimbraPrefIdentityId", grantor.getId()).put("zimbraPrefIdentityName", name).put("zimbraPrefFromDisplay", name).put("zimbraPrefFromAddress", mail).put("objectClass", AttributeClass.OC_zimbraAclTarget).build();
/*     */       
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 122 */       result.add(new Identity(grantee, name, grantor.getId(), attrs, grantee.getProvisioning()));
/*     */     }
/* 124 */     return result.build();
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public static List<ZimbraACE> grantRight(Provisioning prov, Entry target, Set<ZimbraACE> aces)
/*     */     throws ServiceException
/*     */   {
/* 133 */     for (ZimbraACE ace : aces) {
/* 134 */       ZimbraACE.validate(ace);
/*     */     }
/* 136 */     ZimbraACL acl = getACL(target);
/* 137 */     List<ZimbraACE> granted = null;
/*     */     
/* 139 */     if (acl == null) {
/* 140 */       acl = new ZimbraACL(aces);
/* 141 */       granted = acl.getAllACEs();
/*     */ 
/*     */ 
/*     */     }
/*     */     else
/*     */     {
/*     */ 
/* 148 */       acl = acl.clone();
/* 149 */       granted = acl.grantAccess(aces);
/*     */     }
/*     */     
/* 152 */     serialize(prov, target, acl);
/*     */     
/* 154 */     PermissionCache.invalidateCache(target);
/*     */     
/* 156 */     return granted;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static List<ZimbraACE> revokeRight(Provisioning prov, Entry target, Set<ZimbraACE> aces)
/*     */     throws ServiceException
/*     */   {
/* 166 */     ZimbraACL acl = getACL(target);
/* 167 */     if (acl == null) {
/* 168 */       return new ArrayList();
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 175 */     acl = acl.clone();
/* 176 */     List<ZimbraACE> revoked = acl.revokeAccess(aces);
/* 177 */     serialize(prov, target, acl);
/*     */     
/* 179 */     PermissionCache.invalidateCache(target);
/*     */     
/* 181 */     return revoked;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   private static void serialize(Provisioning prov, Entry entry, ZimbraACL acl)
/*     */     throws ServiceException
/*     */   {
/* 190 */     prov.modifyAttrs(entry, Collections.singletonMap("zimbraACE", acl.serialize()));
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   static ZimbraACL getACL(Entry entry)
/*     */     throws ServiceException
/*     */   {
/* 201 */     ZimbraACL acl = (ZimbraACL)entry.getCachedData("ENTRY.ACL_CACHE");
/* 202 */     if (acl != null) {
/* 203 */       return acl;
/*     */     }
/* 205 */     String[] aces = entry.getMultiAttr("zimbraACE");
/* 206 */     if (aces.length == 0) {
/* 207 */       return null;
/*     */     }
/* 209 */     acl = new ZimbraACL(aces, TargetType.getTargetType(entry), entry.getLabel());
/* 210 */     entry.setCachedData("ENTRY.ACL_CACHE", acl);
/*     */     
/*     */ 
/* 213 */     return acl;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/account/accesscontrol/ACLUtil.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */